package org.whh.auth.controller;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.whh.auth.WhhPermission;
import org.whh.auth.domain.Menu;
import org.whh.auth.service.IMenuService;
import org.whh.base.util.AjaxResult;
import org.whh.base.util.ResultDTO;
import org.whh.base.util.QueryVo;

import java.util.List;

@RestController
@WhhPermission(name="菜单管理",desc = "这是菜单管理")
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private IMenuService menuService;

    @GetMapping("/ids")
    public AjaxResult getMenuIds(){
        try {
            List<Long> menuIds = menuService.getMenuIds();
            return AjaxResult.getself().setObj(menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getself().setSuccess(false).setMessage("查询失败!"+e.getMessage());
        }
    }

    @GetMapping("/tree")
    @WhhPermission(name="获取菜单树",desc = "这是获取菜单树")
    public AjaxResult getMenuTree(){
        try {
            List<Menu> menus=menuService.getMenuTree();
            return new AjaxResult().getself().setObj(menus);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("获取菜单树失败"+e.getMessage());

        }
    }

    @GetMapping("/tree/{employeeId}")
//    @WhhPermission(name="通过员工id获取菜单树",desc = "这是通过员工id获取菜单树")
    public AjaxResult getMenuTreeByEmployeeId(@PathVariable("employeeId") Long employeeId){
        try {
            List<Menu> menus=menuService.getMenuTreeByEmployeeId(employeeId);
            System.out.println("menus");
            System.out.println(menus);
            return new AjaxResult().getself().setObj(menus);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("获取菜单树失败"+e.getMessage());

        }
    }

    @PostMapping
    @WhhPermission(name="分页查询菜单",desc = "这是分页查询菜单")
    public AjaxResult getMenuPage(@RequestBody QueryVo queryVo){
        try {
            ResultDTO<Menu> result=menuService.getAllPage(queryVo);
            return new AjaxResult().getself().setObj(result);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("分页查出错"+e.getMessage());
        }
    }

    @PutMapping
    @ApiOperation(value = "添加或者更新方法")
    @WhhPermission(name="添加或者修改",desc = "我是添加或者修改")
    public AjaxResult addOrUpdate(@RequestBody Menu menu){
        try {
            if(menu.getId()!=null){
                menuService.update(menu);
            }else {
                menuService.insert(menu);
            }
            return new AjaxResult().getself();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("操作出错"+e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除方法")
    @WhhPermission(name="删除",desc = "我是删除")
    public AjaxResult delete(@PathVariable("id") @ApiParam(value = "主键",required = true) Long id){
        try {
            menuService.deleteById(id);
            return new AjaxResult().getself();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("删除出错"+e.getMessage());
        }
    }

    @PatchMapping
    @ApiOperation(value = "批量删除方法")
    public AjaxResult batchDelete(@ApiParam(value = "主键",required = true)@RequestBody List<Long> ids){
        try {
            System.out.println("批量删除方法");
            menuService.batchDelete(ids);
            return new AjaxResult().getself();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("删除出错"+e.getMessage());
        }
    }

    @GetMapping
    @ApiOperation(value = "查询所有菜单方法")
    public AjaxResult getAll(){
        try {
            List<Menu> menus = menuService.getAll();
            return new AjaxResult().getself().setObj(menus);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("查询出错"+e.getMessage());
        }
    }


}
